//
//  MYGTopicCommentsTableViewCell.swift
//  MYDoctor
//
//  Created by 王振明 on 2024/4/3.
//

import UIKit

class MYGTopicCommentsTableViewCell: UITableViewCell {
    
    var model = MYGTopicCommentModel()

    var currentSection = 0
    
    var tapComonCallBack:((MYGTopicCommentModel,Int)->())?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        contentView.addSubview(headerImageBV)
        headerImageBV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(60)
            make.top.equalToSuperview().offset(15)
            make.size.equalTo(CGSize(width: 28, height: 28))
        }
        
        contentView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headerImageBV.snp.right).offset(8)
            make.top.equalTo(headerImageBV.snp.top)
            make.height.equalTo(15)
        }
        
        contentView.addSubview(replyIcon)
        replyIcon.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(7)
            make.centerY.equalTo(nameLabel.snp.centerY)
            make.size.equalTo(CGSize(width: 4, height: 7))
        }
        
        contentView.addSubview(replyName)
        replyName.snp.makeConstraints { make in
            make.left.equalTo(replyIcon.snp.right).offset(7)
            make.centerY.equalTo(nameLabel.snp.centerY)
            make.height.equalTo(15)
        }
        contentView.addSubview(contentLab)
        contentLab.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.left)
            make.top.equalTo(nameLabel.snp.bottom)
            make.right.equalToSuperview().offset(-20)
        }
        
        contentView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(contentLab.snp.left)
            make.top.equalTo(contentLab.snp.bottom).offset(3)
            make.height.equalTo(11)
        }
        
        contentView.addSubview(commentBtn)
        commentBtn.snp.makeConstraints { make in
            make.left.equalTo(timeLabel.snp.right).offset(3.5)
            make.centerY.equalTo(timeLabel)
            make.size.equalTo(CGSize(width: 30, height: 30))
        }
        contentView.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.left)
            make.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private lazy var headerImageBV: UIImageView = {
        let cont = UIImageView()
        cont.layer.cornerRadius = 14
        cont.layer.masksToBounds = true
        cont.contentMode = .scaleAspectFill
        return cont
    }()
    private lazy var nameLabel: UILabel = {
        let cont = UILabel()
        cont.text = "武艺星灿"
        cont.font = .regular(10)
        cont.textColor = .rgba(102, 102, 102)
        return cont
    }()
    
    var replyIcon: UIImageView = {
        let control = UIImageView()
        control.isHidden = true
        control.image = UIImage.init(named: "MYG_topic_reply")
        return control
    }()
    
    var replyName: UILabel = {
        let cont = UILabel()
        cont.isHidden = true
        cont.font = .regular(10)
        cont.textColor = .rgba(102, 102, 102)
        return cont
    }()
    private lazy var contentLab: UILabel = {
        let cont = UILabel()
        cont.text = "武艺星灿武艺星灿武艺星灿武艺星灿武艺星灿武艺星灿"
        cont.font = .regular(11)
        cont.textColor = .rgba(51, 51, 51)
        cont.numberOfLines = 0
        return cont
    }()
    private lazy var timeLabel: UILabel = {
        let cont = UILabel()
        cont.text = "2023-01-01"
        cont.font = .regular(8)
        cont.textColor = .rgba(204, 204, 204)
        return cont
    }()
    private lazy var commentBtn: UIButton = {
        let cont = UIButton()
        cont.addTarget(self, action: #selector(tapComment), for: .touchUpInside)
        cont.setImage(UIImage(named: "MYG_home_comment"), for: .normal)
        return cont
    }()
    
    var line: UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hexString: "#eeeeee")
        return control
    }()
    func bindData(_ data:MYGTopicCommentModel) {
        self.model = data
        self.nameLabel.text = data.doctor.name
        self.replyIcon.isHidden = true
        if self.model.patient != nil {
            self.replyIcon.isHidden = false
            self.replyName.text = self.model.patient?.name
        }
        self.headerImageBV.sd_setImage(with: URL(string: data.doctor.avatar), placeholderImage: UIImage.init(named: "MYG_defaultAvatar_doctor"), context: nil)
        self.contentLab.text = data.comment
        self.timeLabel.text = data.created_at
    }
    
    @objc func tapComment() {
        if let c = tapComonCallBack {
            c(self.model,currentSection)
        }
    }
}
